import { ref } from 'vue';

// function reducer(state, action) {
//   const { type, payload } = action;

//   switch (type) {
//     case actionTypes.ADD_TODO:
//       return todoFn.addTodo(state, payload);
//     case actionTypes.TOGGLE_TODO:
//       return todoFn.toggleTodo(state, payload);
//     case actionTypes.REMOVE_TODO:
//       return todoFn.removeTodo(state, payload);
//     default:
//       break;
//   }
// }

function useReducer(reducer, initialState) {
  // initialState 是需要响应式的
  const state = ref(initialState);
  const action = {};
  
  function dispatch({ type, payload }) {
    action.type = type;
    action.payload = payload;

    reducer(state, action);
  }

  return [
    state,
    dispatch
  ]
}

export default useReducer;